package com.study.file.service;

import com.study.common.entity.LoginException;
import com.study.common.entity.User;
import com.study.common.enumerate.ExceptionInfo;
import com.study.common.utils.SafetyUtils;
import com.study.file.entity.Entity;
import com.study.main.entity.Information;
import com.study.main.vo.UserInfo;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.ResourceUtils;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

public class PoiServiceImp implements PoiService {
    public List<Entity> getExcelData(String name) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        Workbook book = null;
        System.out.println("文件名" + name);
        System.out.println(" 文件路径:" + System.getProperty("user.dir") + "/" + name);
        List<Entity> list = new ArrayList<Entity>();
        try {
            book = new XSSFWorkbook(new FileInputStream(ResourceUtils.getFile(System.getProperty("user.dir") + "/" + name)));
        } catch (Exception e) {
            try {
                book = new HSSFWorkbook(new FileInputStream(ResourceUtils.getFile(System.getProperty("user.dir") + "/" + name)));
            } catch (Exception e1) {
                throw new LoginException(ExceptionInfo.FORMAT_ERROR);
            }
        }
        //Excel工作
        try{
            HSSFSheet sheet = (HSSFSheet) book.getSheetAt(0);
            for (int i = 1; i < sheet.getLastRowNum() + 1; i++) {
                HSSFRow row = sheet.getRow(i);
                Entity entity = new Entity();
                User user = new User();
                Information information = new Information();
                String chineseName = row.getCell(0).getStringCellValue();
                String email = row.getCell(1).getStringCellValue();
                String gitlabNumber = row.getCell(2).getStringCellValue();
                String password = row.getCell(3).getStringCellValue();
                String telNumber = row.getCell(4).getStringCellValue();
                String userName = row.getCell(5).getStringCellValue();
                double credit = row.getCell(6).getNumericCellValue();
                double grade = row.getCell(7).getNumericCellValue();
                user.setChineseName(chineseName);
                information.setCredit(credit);
                user.setEmail(email);
                user.setTelNumber(telNumber);
                user.setGitlabNumber(gitlabNumber);
                user.setUserName(userName);
                user.setPassWord(password);
                information.setGrade(grade);
                entity.setUser(user);
                entity.setInformation(information);
                list.add(entity);
            }
        } catch (Exception e){
            try{
                XSSFSheet sheet = (XSSFSheet)book.getSheetAt(0);
                for (int i = 1; i < sheet.getLastRowNum() + 1; i++) {
                    XSSFRow row = sheet.getRow(i);
                    Entity entity = new Entity();
                    User user = new User();
                    Information information = new Information();
                    String chineseName = row.getCell(0).getStringCellValue();
                    String email = row.getCell(1).getStringCellValue();
                    String gitlabNumber = row.getCell(2).getStringCellValue();
                    String password = row.getCell(3).getStringCellValue();
                    XSSFCell cell2 = row.getCell(4);
                    //设置单元格类型
                    cell2.setCellType(CellType.STRING);
                    String telNumber = cell2.getStringCellValue();

                    String userName = row.getCell(5).getStringCellValue();
                    XSSFCell cell = row.getCell(6);
                    //设置单元格类型
                    cell.setCellType(CellType.STRING);
                    double credit = Double.parseDouble(cell.getStringCellValue());
                    XSSFCell cell1 = row.getCell(7);
                    //设置单元格类型
                    cell1.setCellType(CellType.STRING);
                    double grade = Double.parseDouble(cell1.getStringCellValue());
                    user.setChineseName(chineseName);
                    information.setCredit(credit);
                    user.setEmail(email);
                    user.setTelNumber(telNumber);
                    user.setGitlabNumber(gitlabNumber);
                    user.setUserName(userName);
                    user.setPassWord(password);
                    information.setGrade(grade);
                    entity.setUser(user);
                    entity.setInformation(information);
                    list.add(entity);
                }}catch (Exception e1){
                throw new LoginException(ExceptionInfo.FORMAT_ERROR);
            }
            }
        return list;
    }

}
